package cn.ac.cintcm.webapp.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.ac.cintcm.Constants;
import cn.ac.cintcm.model.FeedBack;
import cn.ac.cintcm.model.Therapy;
import cn.ac.cintcm.model.User;
import cn.ac.cintcm.service.FeedbackManager;
import cn.ac.cintcm.service.TherapyManager;
import cn.ac.cintcm.service.UserManager;
import cn.ac.cintcm.util.AuthorityUtil;

public class TherapyController extends MultiActionController {

    private final Log log = LogFactory.getLog(TherapyController.class);
    
    private TherapyManager therapyManager = null;

	private UserManager userManager = null;

	private FeedbackManager feedbackManager = null;
	
	private AuthorityUtil authorityUtil = null;

    public void setTherapyManager(TherapyManager therapyManager) {
        this.therapyManager = therapyManager;
    }
    
    public void setUserManager(UserManager um) {
    	this.userManager = um;
    }
    
    public void setFeedBackManager(FeedbackManager feedbackManager) {
    	this.feedbackManager  = feedbackManager;
    }
    
    public void setAuthorityUtil(AuthorityUtil authorityUtil) {
    	this.authorityUtil = authorityUtil;
    }

	public ModelAndView therapys(HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("entering 'TherapyController handleRequest' method...");
		String username = request.getRemoteUser();
		
        return new ModelAndView("therapys").addObject("therapyList", therapyManager.getDoctorTherapies(username));

	}
	
	public ModelAndView patient(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String uid = request.getParameter("u");
		ModelAndView mv = null;
		if (StringUtils.isNotBlank(uid)) {
			Long userId = new Long(uid);
			if (authorityUtil.hasPrivilegeById(request, response, uid)) {
				log.debug("enetering autority");
				mv = new ModelAndView("patient");
				if (!request.isUserInRole(Constants.DOCTOR_ROLE)) {
					mv = new ModelAndView("patientMenu");
				}
				User u = userManager.getUser(uid);
				mv.addObject("user", u);
				
				Therapy therapy = therapyManager.getLastTherapy(userId);
				therapy.getFeedbacks();
				mv.addObject("therapy", therapy);
				
				List<Therapy> therapyList = therapyManager.getHistoryTherapy(userId);
				mv.addObject("therapyList", therapyList);
			}
		}
        return mv;

	}	
	
	public ModelAndView checkpatient(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String tid = request.getParameter("t");
		ModelAndView mv = null;
		if (StringUtils.isNotBlank(tid)) {
			Long tId = new Long(tid);
			FeedBack fb = feedbackManager.get(tId);
			fb.setChecked(true);
			feedbackManager.save(fb);
			return patient(request, response);
		}
        return mv;

	}	
	
	public ModelAndView therapydetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String userId = request.getParameter("u");
		
		String parentId = request.getParameter("p");
		List<Therapy> therapyList = null;
		if (StringUtils.isNotEmpty(userId) && StringUtils.isNotEmpty(parentId)) {
			Long id = new Long(userId);
			if (authorityUtil.hasPrivilegeById(request, response, userId)) {
				therapyList = therapyManager.getTherapyThread(id, new Long(parentId));	
				if (therapyList != null) {
					for (Therapy t : therapyList) {
						List<FeedBack> feedbacks = feedbackManager.getByTherapy(t.getId());
						t.setFeedbacks(feedbacks);
					}
				}
			}
		}

		ModelAndView mv = new ModelAndView("therapydetail");
		mv.addObject("therapyList", therapyList);
		mv.addObject("userId", userId);

        return mv;

	}	

	public ModelAndView feedbackdetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String therapyId = request.getParameter("id");
		ModelAndView mv = new ModelAndView("feedbackdetail");

		if (StringUtils.isNotEmpty(therapyId)) {
			Long id = new Long(therapyId);
			Therapy therapy = therapyManager.get(id);
			if (therapy.getDoctor().getUsername().equals(request.getRemoteUser()) ||
					therapy.getPatient().getUsername().equals(request.getRemoteUser())) {
				mv.addObject("therapy", therapy);
				User user = therapy.getPatient();
				mv.addObject("user", user);
				List<FeedBack> feedbacks = therapy.getFeedbacks();
				mv.addObject("feedbackList", feedbacks);
			}
		}
        return mv;

	}	
}
